/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */
package com.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.domain.Announcement;
import com.domain.Email;
import com.domain.Event;
import com.domain.User;
import com.forms.ApproveForm;
import com.forms.ForwardForm;
import com.util.CommunityEmailer;

public class ApproveAction extends CommunityBaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);
		
		//This action the form and approves any data element that has been selected
		//This action can also be used to deny or delete a data element
		ApproveForm f = (ApproveForm)form;
		ActionForward forward = mapping.findForward("success");
		CommunityEmailer comEmail = this.biz.getCommunityEmailer();
		
		if(f.getObjKeyId()!=null&&Long.parseLong(f.getObjKeyId())>0){
			
			//Approving user
			if(f.getType().equals("user")){
				Long uKeyId = Long.parseLong(f.getObjKeyId());
				User user = this.biz.getPrivateMgr().getUserByKeyId(uKeyId);
				if(user!=null){
					if(f.getApproved()!=null&&f.getApproved().equals("true")){
						user.setApproved(true);
						user = this.biz.getPrivateMgr().getUserDAO().save(user);
						if(user!=null){
							try{
								comEmail.sendMail(user.getEmail(),"Welcome to the Community Project at USC", "Your account information is below." +
										"\nUsername: "+user.getUsername()+"\nPassword: "+user.getPassword()+"\nThank you for the interest in our site.");
								request.setAttribute("INFO", "User '"+user.getUsername()+"' was successfully approved and notified.");
							}catch(Exception e){
								request.setAttribute("ERRORS", "Problem sending user notification.");
								forward = mapping.findForward("failure");
							}
						}
						else{
							request.setAttribute("ERRORS", "Problem approving user.");
							forward = mapping.findForward("failure");
						}	
					}
					else{
						user.setDeleted(true);
						user = this.biz.getPrivateMgr().getUserDAO().save(user);
						if(user!=null){
							request.setAttribute("INFO", "User '"+user.getUsername()+"' was successfully denied and deleted.");
						}
						else{
							request.setAttribute("ERRORS", "Problem denying user.");
							forward =  mapping.findForward("failure");
						}
						
					}
				}
				else{
					request.setAttribute("ERRORS", "Problem approving user.");
					forward =  mapping.findForward("failure");
				}
			}
			
			//Approving event
			else if(f.getType().equals("event")){
				Long eKeyId = Long.parseLong(f.getObjKeyId());
				Event event = this.biz.getPrivateMgr().getEventByKeyId(eKeyId);
				if(event!=null){
					if(f.getApproved()!=null&&f.getApproved().equals("true")){
						event.setApproved(true);
						event = this.biz.getPrivateMgr().getEventDAO().save(event);
						if(event!=null){
							request.setAttribute("INFO", "Event '"+event.getEventTitle()+"' was successfully approved.");
						}
						else{
							request.setAttribute("ERRORS", "Problem approving event.");
							forward =  mapping.findForward("failure");
						}	
					}
					else{
						event.setDeleted(true);
						event = this.biz.getPrivateMgr().getEventDAO().save(event);
						if(event!=null){
							request.setAttribute("INFO", "Event '"+event.getEventTitle()+"' was successfully denied and deleted.");
						}
						else{
							request.setAttribute("ERRORS", "Problem denying event.");
							forward =  mapping.findForward("failure");
						}
					}
				}
				else{
					request.setAttribute("ERRORS", "Problem approving event.");
					forward =  mapping.findForward("failure");
				}			
			}
			
			//Approving email
			else if(f.getType().equals("email")){
				Long eKeyId = Long.parseLong(f.getObjKeyId());
				Email email = this.biz.getPrivateMgr().getEmailByKeyId(eKeyId);
				if(email!=null){
					if(f.getApproved()!=null&&f.getApproved().equals("true")){
						email.setApproved(true);
						email = this.biz.getPrivateMgr().getEmailDAO().save(email);
						if(email!=null){
							request.setAttribute("INFO", "Email '"+email.getEmailSubject()+"' was successfully approved and sent.");
							comEmail.addEmail(email.getKeyId());
						}
						else{
							request.setAttribute("ERRORS", "Problem approving email.");
							forward =  mapping.findForward("failure");
						}	
					}
					else{
						email.setDeleted(true);
						email = this.biz.getPrivateMgr().getEmailDAO().save(email);
						if(email!=null){
							request.setAttribute("INFO", "Email '"+email.getEmailSubject()+"' was successfully denied and deleted.");
						}
						else{
							request.setAttribute("ERRORS", "Problem denying email.");
							forward =  mapping.findForward("failure");
						}
					}
				}
				else{
					request.setAttribute("ERRORS", "Problem approving email.");
					forward =  mapping.findForward("failure");
				}
			}
			
			//Approving announcements
			else if(f.getType().equals("announcement")){
				Long aKeyId = Long.parseLong(f.getObjKeyId());
				Announcement announcement = this.biz.getPrivateMgr().getAnnouncementByKeyId(aKeyId);
				if(announcement!=null){
					if(f.getApproved()!=null&&f.getApproved().equals("true")){
						announcement.setApproved(true);
						announcement = this.biz.getPrivateMgr().getAnnouncementDAO().save(announcement);
						if(announcement!=null){
							request.setAttribute("INFO", "Announcement '"+announcement.getAnnouncementTitle()+"' was successfully approved.");
						}
						else{
							request.setAttribute("ERRORS", "Problem approving announcement.");
							forward =  mapping.findForward("failure");
						}	
					}
					else{
						announcement.setDeleted(true);
						announcement = this.biz.getPrivateMgr().getAnnouncementDAO().save(announcement);
						if(announcement!=null){
							request.setAttribute("INFO", "Announcement '"+announcement.getAnnouncementTitle()+"' was successfully denied and deleted.");
						}
						else{
							request.setAttribute("ERRORS", "Problem denying announcement.");
							forward =  mapping.findForward("failure");
						}
					}
				}
				else{
					request.setAttribute("ERRORS", "Problem approving announcement.");
					forward =  mapping.findForward("failure");
				}
			}
			else{
				request.setAttribute("ERRORS", "Invalid request.  Check credentials.");
				forward =  mapping.findForward("failure");
			}
		}
		
		//Re-quering to show update lists on display page
		List<User> userResults = this.biz.getPrivateMgr().getPendingUsers();
		if(userResults!=null&&userResults.size()>0)request.setAttribute("APPROVE_USERS", userResults);
		List<Announcement> announcementResults = this.biz.getPrivateMgr().getPendingAnnouncements();
		if(announcementResults!=null&&announcementResults.size()>0)request.setAttribute("APPROVE_ANNOUNCEMENTS", announcementResults);
		List<Event> eventResults = this.biz.getPrivateMgr().getPendingEvents();
		if(eventResults!=null&&eventResults.size()>0)request.setAttribute("APPROVE_EVENTS", eventResults);
		List<Email> emailResults = this.biz.getPrivateMgr().getPendingEmails();
		if(emailResults!=null&&emailResults.size()>0)request.setAttribute("APPROVE_EMAILS", emailResults);

		

		return forward;//forward;
	}

}
